package org.duoyi.striver.service.task;

import lombok.extern.slf4j.Slf4j;
import org.duoyi.striver.StriverApplication;
import org.duoyi.striver.consts.Area;
import org.duoyi.striver.consts.Picture;
import org.duoyi.striver.model.Point;
import org.duoyi.striver.model.impl.Sleeper;
import org.duoyi.striver.service.common.CommonService;

import java.util.ArrayList;
import java.util.List;

/**
 * 英雄试炼
 */
@Slf4j
public class TrialTask extends AbstractTask {

    private static final List<Area> areas = new ArrayList<>();

    private static final List<Picture> pictures = new ArrayList<>();

    static {
        for (int i = 7; i > 1; i--) {
            areas.add(Area.valueOf("英雄试炼_场景" + i));
        }

        pictures.add(Picture.英雄试炼_东海龙宫_未完成标识);
        pictures.add(Picture.英雄试炼_天庭_未完成标识);
        pictures.add(Picture.英雄试炼_流沙河_未完成标识);
        pictures.add(Picture.英雄试炼_火焰山_未完成标识);
        pictures.add(Picture.英雄试炼_狮驼岭_未完成标识);
        pictures.add(Picture.英雄试炼_花果山_未完成标识);
        pictures.add(Picture.英雄试炼_高老庄_未完成标识);
    }

    @Override
    public void doTask() {
        if (!before()) {
            return;
        }

        //进入到试炼界面
        for (Area area : areas) {
            String sceneName;
            synchronized (StriverApplication.class) {
                log.info("选择场景");
                area.leftClick();
                Sleeper.sleep(5000);
                Picture scene = null;
                for (Picture picture : pictures) {
                    if (picture.find()) {
                        scene = picture;
                    }
                }
                if (scene == null) {
                    log.info("场景不可挑战");
                    continue;
                }
                sceneName = scene.name().split("_")[1];
                log.info("挑战当前场景，当前场景是【{}】", sceneName);
                Area.valueOf(scene.name().replace("_未完成标识", "") + "_选择").leftClick();
                Area.英雄试炼_场景选择按钮.leftClick();
            }
            for (int i = 1; i <= 5; i++) {
                synchronized (StriverApplication.class) {
                    Area item = Area.valueOf("英雄试炼_" + sceneName + "_" + i);
                    while (!CommonService.isFighting()) {
                        item.leftClick();
                        log.info("准备挑战第{}个对手", i);
                        Area.英雄试炼_挑战按钮.leftClick();
                        Sleeper.sleep(2000);
                    }
                }
            }

            synchronized (StriverApplication.class) {
                for (int i = 0; i < 20; i++) {
                    Point point = Picture.英雄试炼_宝箱.findPoint();
                    if (point != null) {
                        log.info("拣宝箱");
                        point.leftClick();
                        break;
                    }
                    Sleeper.sleep(100);
                }
            }

            synchronized (StriverApplication.class) {
                if (Picture.英雄试炼_异域挑战令.find()) {
                    Area.英雄试炼_异域挑战令.leftClick();
                    Sleeper.sleep(3000);
                    Area.英雄试炼_挑战按钮.leftClick();
                    CommonService.isFighting();
                }
            }

            log.info("退出试炼界面");

            Area.英雄试炼_退出按钮.leftClick();
            break;
        }
    }

    private boolean before() {
        synchronized (StriverApplication.class) {
            CommonService.openDailyPanel();

            if (Picture.英雄试炼_已完成.find()) {
                return false;
            }
            Area.英雄试炼_日程领取.leftClick();
            log.info("进入试炼界面");
            Area.英雄试炼_对话领取.leftClick();
            return true;
        }
    }
}
